//
//  DropDownView.m
//  JiuLe
//
//  Created by xcq on 15/12/2.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "DropDownView.h"
#import "CLMarcoDefine.h"
#import "UIColor+RGBConverHex.h"
#import "JLDropDownTableViewCell.h"
@implementation DropDownView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self viewDidLoad];
    }
    return self;
}

- (void)viewDidLoad {
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"box"]];
    img.frame = CGRectMake(0, 0, self.frame.size.width + 10 , self.frame.size.height+20);
    img.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:img];
    
    dataSource = [NSMutableArray arrayWithArray:@[@"全部",@"紧急呼叫",@"健康报警",@"健康预警",@"跌倒报警"]];
    tableView = [[UITableView alloc] initWithFrame:(CGRect){ {8,20},{ self.bounds.size.width - 8
        , self.bounds.size.height-15}} style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.dataSource = self;
    tableView.rowHeight = 32;
    tableView.scrollEnabled = NO;
    
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    [self addSubview:tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JLDropDownTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"JLDropDownTableViewCell" owner:self options:nil] lastObject];
    cell.title.text = dataSource[indexPath.row];
    cell.title.font = iOS8Font(14.f);
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
    _action(indexPath.row , dataSource[indexPath.row]);
}

@end
